package com.xyazm.order.service.impl;

import com.alibaba.fastjson.JSON;
import com.xyazm.common.enums.OrderStatusChangeEnum;
import com.xyazm.common.enums.RocketMqTopicEnum;
import com.xyazm.common.message.PaidOrderSuccessMessage;
import com.xyazm.fulfill.domain.event.OrderDeliveredEvent;
import com.xyazm.fulfill.domain.event.OrderOutStockEvent;
import com.xyazm.fulfill.domain.event.OrderSignedEvent;
import com.xyazm.fulfill.domain.param.TriggerFulFillParam;
import com.xyazm.fulfill.domain.param.TriggerOrderAfterFulfillEventParam;
import com.xyazm.order.dao.OrderInfoDAO;
import com.xyazm.order.domain.entity.OrderInfoDO;
import com.xyazm.order.mq.producer.DefaultProducer;
import com.xyazm.order.remote.FulfillRemote;
import com.xyazm.order.service.IOrderFulFillService;
import com.xyazm.order.service.IOrderTriggerService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


@Slf4j
@Service
public class OrderTriggerServiceImpl implements IOrderTriggerService {

    @Autowired
    private FulfillRemote fulfillRemote;

    @Autowired
    private DefaultProducer defaultProducer;

    @Autowired
    private OrderInfoDAO orderInfoDAO;

    @Autowired
    private IOrderFulFillService orderFulFillService;

    @Override
    public void triggerOrderOutStockWmsEvent(OrderStatusChangeEnum orderStatusChangeEnum,
                                             OrderOutStockEvent event) {
        TriggerOrderAfterFulfillEventParam param = new TriggerOrderAfterFulfillEventParam();
        param.setOrderStatusChange(orderStatusChangeEnum);
        param.setAfterFulfillEvent(event);
        fulfillRemote.triggerOrderWmsShipEvent(param);
    }

    @Override
    public void triggerOrderDeliveredWmsEvent(OrderStatusChangeEnum orderStatusChangeEnum,
                                              OrderDeliveredEvent event) {
        TriggerOrderAfterFulfillEventParam param = new TriggerOrderAfterFulfillEventParam();
        param.setOrderStatusChange(orderStatusChangeEnum);
        param.setAfterFulfillEvent(event);
        fulfillRemote.triggerOrderWmsShipEvent(param);
    }

    @Override
    public void triggerOrderSignedWmsEvent(OrderStatusChangeEnum orderStatusChangeEnum,
                                           OrderSignedEvent event) {
        TriggerOrderAfterFulfillEventParam param = new TriggerOrderAfterFulfillEventParam();
        param.setOrderStatusChange(orderStatusChangeEnum);
        param.setAfterFulfillEvent(event);
        fulfillRemote.triggerOrderWmsShipEvent(param);
    }

    @Override
    public void triggerReceiveOrderFulFill(String orderId, String fulfillException, String wmsException, String tmsException) {
        OrderInfoDO orderInfoDO = orderInfoDAO.getByOrderId(orderId);
        TriggerFulFillParam param = orderFulFillService.buildTriggerFulFillParam(orderInfoDO);
        param.setFulfillException(fulfillException);
        param.setWmsException(wmsException);
        param.setTmsException(tmsException);
        fulfillRemote.triggerFulFill(param);
    }


    @Override
    public void triggerOrderPaidEvent(String orderId) {
        PaidOrderSuccessMessage message = new PaidOrderSuccessMessage();
        message.setOrderId(orderId);
        String msgJson = JSON.toJSONString(message);
        defaultProducer.sendMessage(RocketMqTopicEnum.PAID_ORDER_SUCCESS.getTopic(), msgJson, "订单已完成支付",null, orderId);
    }

}
